<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Factory;
use think\Db;

use think\Config;
use think\Validate;//验证
use fast\Random;
use fast\Http;

use app\api\controller\Ltd;



/**
 * 公司人员🏚
 */
class Ltder extends Ltd
{

    protected $noNeedLogin = ['help'];
    protected $noNeedRight = ['*'];

    protected $db = 'ltder';

    public function _initialize()
    {
        parent::_initialize();

    }


    
    /**
     * 说明文档
     *
     * @return void
     */
    public function help()
    {
        $this->success('公司角色才可以调此接口');
    }

    /**
     * 获取申请信息
     *
     * @return void
     */
    public function get_apply_data()
    {
        $this->success('ok',Db::name($this->db)->where('uid',$this->auth->id)->find());
    }



    /**
     * 申请成为公司员工🧑🏻‍🚒
     * 
     * @param string $tel 电话
     * @param string $ltd_id 公司id
     * @param string $name 姓名
     * @param string $wechat 微信号
     * @param string $email 邮箱
     * @param string $ps 备注
     * @return void
     */
    public function apply_ltder_staff()
    {
        $data['uid'] = $this->auth->id;
        // $data['pid'] = $this->request->param('pid','');    
        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $this->error('已提交过');
        }

        $data['name'] = $this->request->param('name','');
        $data['wechat'] = $this->request->param('wechat','');
        $data['email'] = $this->request->param('email','');
        $data['ps'] = $this->request->param('ps','');   
        $data['tel'] = $this->request->param('tel');

        $data['ltd_id'] = $this->request->param('ltd_id');
        $data['admin_id'] = $this->GetAdminIdByLtdId($data['ltd_id']);//

        $data['role_status'] = '2';   //角色:1=主管,2=员工,3=兼职人员
        
        $data['time'] = time();
        $data['status_switch'] = 0;
        $res = Db::name($this->db)->insertGetId($data);
        if($res){
            $this->success('申请成功');
        }
    }


    /**
     * 申请成为公司兼职人员🧑🏻‍🚒
     * 
     * @param string $tel 电话
     * @param string $ltd_id 公司id
     * @param string $name 姓名
     * @param string $wechat 微信号
     * @param string $email 邮箱
     * @param string $ps 备注
     * @return void
     */
    public function apply_ltder_job()
    {
        $data['uid'] = $this->auth->id;
        // $data['pid'] = $this->request->param('pid','');    
        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $this->error('已提交过');
        }

        $data['name'] = $this->request->param('name','');
        $data['wechat'] = $this->request->param('wechat','');
        $data['email'] = $this->request->param('email','');
        $data['ps'] = $this->request->param('ps','');   
        $data['tel'] = $this->request->param('tel');

        $data['ltd_id'] = $this->request->param('ltd_id');
        $data['admin_id'] = $this->GetAdminIdByLtdId($data['ltd_id']);//

        $data['role_status'] = '3';   //角色:1=主管,2=员工,3=兼职人员
        
        $data['time'] = time();
        $data['status_switch'] = 0;
        $res = Db::name($this->db)->insertGetId($data);
        if($res){
            $this->success('申请成功');
        }
    }



    // /**
    //  * 修改申请信息
    //  *
    //  * @param string $name 姓名
    //  * @param string $wechat 微信号
    //  * @param int $ltd_id 公司id
    //  * @param string $email 邮箱
    //  * @param string $tel 电话
    //  * @param string $inviter 邀请人
    //  * @param string $ps 备注
    //  * @return void
    //  */ 
    // public function update_apply()
    // {   
    //     $data = Db::name($this->db)->where('uid',$this->auth->id)->find();
    //     if(!$data){
    //         $this->error('你还没申请呢');
    //     }
    //     // if($data['status_switch']==1){
    //     //     $this->error('已通过不能修改了呢');
    //     // }

    //     $data['name'] = $this->request->param('name');
    //     $data['wechat'] = $this->request->param('wechat');
    //     $data['ltd_id'] = $this->request->param('ltd_id');
    //     $data['email'] = $this->request->param('email');
    //     $data['tel'] = $this->request->param('tel');
    //     // $data['pid'] = $this->request->param('pid');        
    //     $data['ps'] = $this->request->param('ps');     

    //     $data['admin_id'] = $this->GetAdminIdByLtdId($data['ltd_id']);//
    //     $u['uid'] = $this->auth->id;


    //     $res = Db::name($this->db)->where($u)->update($data);
    //     if($res){
    //         $this->success('修改成功');
    //     }
    //     $this->error('修改失败');
    // }

    /**
     * 获取我的提成列表
     *
     * @param int $page 1
     * @param int $limit 10
     * @return void
     */
    public function get_commission_list($page=1,$limit=10)
    {
        $this->is_agree();
        if(Config::get('look_commission')){
            $list = Db::name('commission')->where('uid',$this->auth->id)->page($page,$limit)->select();
            $this->success('ok',$list);
        }else{
            $this->error('无法查看提成记录');
        }
    }

    /**
     * 获取公司信息
     *
     * @return void
     */
    public function get_ltd_data()
    {
        $this->is_zg();
        $ltd = Db::name('ltd')->find($this->user['ltder']['ltd_id']);
        // return $ltd;
        $this->success('ok',$ltd);
    }



    /**
     * 获取我的公司id
     *
     * @return void
     */
    public function get_me_ltd_id()
    {
        $ltd_id = $this->user['ltder']['ltd_id'];
        if($ltd_id>0){
            return $ltd_id;
        }else{
            $this->error('没有公司id');
        }
    }

    /**
     * 修改查看提成状态
     *
     * 
     * @return void
     */
    public function update_look_status()
    {
        $this->is_zg();
        $ltd = Db::name('ltd')->find($this->user['ltder']['ltd_id']);
        if($ltd){
            if($ltd['look_switch']==1){
                $data['look_switch'] = 0;
            }else{
                $data['look_switch'] = 1;
            }
            $res = Db::name('ltd')->where('id',$this->user['ltder']['ltd_id'])->update($data);
            if($res){
                $this->success('修改成功',$data);
            }
        }
    }





    /**
     * 主管获取下级人员列表🤦🏼
     * @param int $page 1
     * @param int $limit 10
     * @return void
     */
    public function get_ltder_list($page=1,$limit=1000)
    {

        $this->is_zg();
        $list = Db::name('ltder')
                ->where('ltd_id',$this->user['ltder']['ltd_id'])
                ->page($page,$limit)
                ->select();
        foreach ($list as $key => &$val) {
            $val['user'] = Db::name('user')->where('id',$val['uid'])->find();
            $val['money'] = $this->GetCommissionMoneyData($val['uid'])['money'];
        }
        $this->success('ok',$list);
    }


    /**
     * 主管获取员工的客户列表🤦🏼
     * @param integer $uid 员工uid
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function zg_get_my_clients_list($uid,$page=1,$limit=10)
    {
        $this->is_zg();
        $list = Db::name('clients')
                ->where('pid',$uid)
                ->order('time desc')
                ->page($page,$limit)
                ->select();

        $this->success('ok',$list);
    }

    /**
     * 主管获取公司员工的客户需求列表🤦🏼
     *
     * @param integer $uid 员工uid
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $status 状态:1=未成交,2=成交
     * @return void
     */
    public function zg_get_clients_need_list($uid,$page=1,$limit=10,$status=null)
    {
        $this->is_zg();
        $where = null;
        if($status){
            $where['status'] = $status;
        }

        $list = Db::name('clients_need')
                ->where('pid',$uid)
                ->where($where)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
            
        foreach ($list as $key => &$val) {
            $val['clients'] = $this->GetClients($val['clients_id']);
            $val['tags'] = $this->TagIdsToArray($val['tag_ids']);
        }

        $this->success('ok',$list);
    }



    /**
     * 主管获取某个客户的需求列表🤦🏼
     * 
     * @param integer $clients_id 客户id 
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $status 状态:1=未成交,2=成交
     * @return void
     */
    public function get_clients_need_list($clients_id,$page=1,$limit=10,$status=null)
    {
        $where = null;
        if($status){
            $where['status'] = $status;
        }

        $list = Db::name('clients_need')
                ->where('clients_id',$clients_id)
                ->where($where)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
            
        foreach ($list as $key => &$val) {
            $val['clients'] = $this->GetClients($val['clients_id']);
            $val['tags'] = $this->TagIdsToArray($val['tag_ids']);
        }

        $this->success('ok',$list);
    }





    

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp       
    

    /**
     * 我的团队获取一级列表信息🍀
     *
     * @return void
     */
    public function get_one_level_ltder_list()
    {
        $list = array();
        $Total_commission = 0;
        $uids = $this->GetOneLevelUid($this->auth->id);
        if($uids){
            foreach($uids as $key=>$uid){
                unset($temp);
                $temp['user'] = Db::name('user')->find($uid);
                $temp['ltder'] = Db::name('ltder')->where('uid',$uid)->find();
                $temp['invite'] = sizeof($this->GetOneLevelUid($uid));//获取直接邀请的用户
                $temp['commisson'] = $this->GetCommissionTotal($uid);//获取提成总金额
                $temp['order'] = $this->GetCommissionNum($uid);


                $list[] = $temp;
                $Total_commission = $Total_commission + $temp['commisson'];//总提成
            } 
            
        }

        $data['list'] = $list;
        $data['total_commission'] = $Total_commission;
        $this->success('one',$data);
    }

    /**
     * 我的团队获取二级列表信息🍀
     *
     * @return void
     */
    public function get_two_level_ltder_list()
    {
        $list = array();
        $Total_commission = 0;
        $uids = $this->LtderGetTwoLevelUid($this->auth->id);
        if($uids){
            foreach($uids as $key=>$uid){
                unset($temp);
                $temp['user'] = Db::name('user')->find($uid);
                $temp['ltder'] = Db::name('ltder')->where('uid',$uid)->find();
                $temp['invite'] = sizeof($this->GetOneLevelUid($uid));//获取直接邀请的用户
                $temp['commisson'] = $this->GetCommissionTotal($uid);//获取提成总金额
                $temp['order'] = $this->GetCommissionNum($uid);


                $list[] = $temp;
                $Total_commission = $Total_commission + $temp['commisson'];//总提成
            } 
            
        }

        $data['list'] = $list;
        $data['total_commission'] = $Total_commission;
        $this->success('one',$data);
    }



    /**
     * 获取我的房源列表
     * @param int $page 1
     * @param int $limit 10
     * @param string $now_status 现房:1=现房,2=准现房,3=新房,4=商铺,5=租房,6=二手房
     * @return void
     */
    public function get_me_goods_list($now_status = null,$page=1,$limit=10)
    {
        $where['uid'] = $this->auth->id;
        if($now_status){
            $where['now_status'] = ['=',$now_status];
        }
        $list = Db::name('goods')
                    ->where($where)
                    ->order('time desc')
                    ->page($page,$limit)
                    ->select();

                    
        $this->success('one',$list);
    }

    /**
     * 获取我公司的房源列表🏢
     * @param int $page 1
     * @param int $limit 10
     * @param string $now_status 现房:1=现房,2=准现房,3=新房,4=商铺,5=租房,6=二手房
     * @param string $area 区域：钦南区、钦北区、河东区、河西区
     * @return void
     */
    public function get_ltd_goods_list($now_status = null,$page=1,$limit=10,$area=null)
    {
        $where['ltd_id'] = $this->user['ltder']['ltd_id'];
        if($now_status){
            $where['now_status'] = ['=',$now_status];
        }
        if($area){
            $where['area'] = ['like', $area];
        }
        $list = Db::name('goods')
                    ->where($where)
                    ->order('time desc')
                    ->page($page,$limit)
                    ->select();

                    
        $this->success('one',$list);
    }


}